using System;
using Firestarter.Test.Base;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO
{
    /// <summary>
    /// Provides a place to write custom tests for DMClass objects.
    /// This file is only written once and can be changed.  The Def file
    /// attached to this as a dependent is rewritten with each regeneration
    /// and contains the standard tests for DMClass.
    /// Regenerate this test project whenever there have been changes to the
    /// business objects.
    /// If tests are failing due to a unique setup in your application,
    /// you can either override the Create methods in TestUtils, or you
    /// can add the test to the ignore list below and reimplement it here.
    /// </summary>
    [TestFixture]
    public class TestDMClassDuplicateRelationshipNameRule
    {
        [SetUp]
        public void SetupTest()
        {
            //Runs every time that any testmethod is executed
            TestUtilsDomain.SetupTest();
            BORegistry.DataAccessor = new DataAccessorInMemory();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //Code that is executed before any test is run in this class. If multiple tests
            // are executed then it will still only be called once.
            TestUtilsDomain.SetupFixture();
        }

        [TearDown]
        public void TearDownTest()
        {
            //runs every time any testmethod is complete
        }

        #region DMClassDuplicateRelationshipNameRule

        [Test]
        public void Test_CreateDMClassDuplicateRelationshipNameRule()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IBusinessObjectRule rule = new DMClassDuplicateRelationshipNameRule();
            //---------------Test Result -----------------------
            Assert.IsNotNull(rule);
            Assert.AreEqual("DMClassDuplicateRelationshipName", rule.Name);
            Assert.AreEqual(ErrorLevel.Warning, rule.ErrorLevel);
        }

//        [Test]
//        public void Test_CreateDMClassDuplicateRelationshipNameRule_WhenClassNull_ShouldRaiseError()
//        {
//            //---------------Set up test pack-------------------
//            //---------------Assert Precondition----------------
//            //---------------Execute Test ----------------------
//            try
//            {
//                new DMClassDuplicateRelationshipNameRule(null);
//                Assert.Fail("expected ArgumentNullException");
//            }
//            //---------------Test Result -----------------------
//            catch (ArgumentNullException ex)
//            {
//                StringAssert.Contains("Value cannot be null", ex.Message);
//                StringAssert.Contains("dmClass", ex.ParamName);
//            }
//        }

        [Test]
        public void Test_IsValid_False_WhenDuplicateRelationshipName()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            DMRelationship dmRelationship = dmClass.OwnerRelationships.CreateBusinessObject();
            DMRelationship dmRelationship2 = dmClass.OwnerRelationships.CreateBusinessObject();
            string relationshipName = TestUtilsShared.GetRandomString();
            dmRelationship.RelationshipName = relationshipName;
            dmRelationship2.RelationshipName = relationshipName;
            IBusinessObjectRule rule = new DMClassDuplicateRelationshipNameRule();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmClass);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            StringAssert.Contains("The Entity/Class '", rule.Message);
            StringAssert.Contains("'" + dmClass.ToString() + "'", rule.Message);
            StringAssert.Contains("' cannot have more than one relationship with the same name('", rule.Message);
        }

        [Test]
        public void Test_IsValid_True_WhenNoRelationships()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
          IBusinessObjectRule rule = new DMClassDuplicateRelationshipNameRule();
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, dmClass.OwnerRelationships.Count);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmClass);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_IsValid_True_WhenOneRelationship()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            dmClass.OwnerRelationships.CreateBusinessObject();
            IBusinessObjectRule rule = new DMClassDuplicateRelationshipNameRule();
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, dmClass.OwnerRelationships.Count);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmClass);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_IsValid_True_WhenNoDuplicateRelationshipName()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            DMRelationship dmRelationship = dmClass.OwnerRelationships.CreateBusinessObject();
            DMRelationship dmRelationship2 = dmClass.OwnerRelationships.CreateBusinessObject();
            string relationshipName = TestUtilsShared.GetRandomString();
            dmRelationship.RelationshipName = relationshipName;
            dmRelationship2.RelationshipName = relationshipName + "ss";
            IBusinessObjectRule rule = new DMClassDuplicateRelationshipNameRule();
            //---------------Assert Precondition----------------
            Assert.AreNotEqual(dmRelationship.RelationshipName, dmRelationship2.RelationshipName);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmClass);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_IsValid_True_WhenRelationshipNameEmpty()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            DMRelationship dmRelationship = dmClass.OwnerRelationships.CreateBusinessObject();
            DMRelationship dmRelationship2 = dmClass.OwnerRelationships.CreateBusinessObject();
            string relationshipName = TestUtilsShared.GetRandomString();
            dmRelationship.RelationshipName = "";
            dmRelationship2.RelationshipName = relationshipName;
            IBusinessObjectRule rule = new DMClassDuplicateRelationshipNameRule();
            //---------------Assert Precondition----------------
            Assert.AreNotEqual(dmRelationship.RelationshipName, dmRelationship2.RelationshipName);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmClass);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        [Test]
        public void Test_IsValid_False_WhenDuplicateRelationshipName_AlsoHasRelationshipNameEmpty()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            DMRelationship dmRelationship = dmClass.OwnerRelationships.CreateBusinessObject();
            DMRelationship dmRelationship2 = dmClass.OwnerRelationships.CreateBusinessObject();
            DMRelationship dmRelationship3 = dmClass.OwnerRelationships.CreateBusinessObject();
            string relationshipName = TestUtilsShared.GetRandomString();
            dmRelationship.RelationshipName = relationshipName;
            dmRelationship2.RelationshipName = relationshipName;
            IBusinessObjectRule rule = new DMClassDuplicateRelationshipNameRule();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmClass);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            StringAssert.Contains("The Entity/Class '", rule.Message);
            StringAssert.Contains("'" + dmClass.ToString() + "'", rule.Message);
            StringAssert.Contains("' cannot have more than one relationship with the same name('", rule.Message);
        }

        [Test]
        public void Test_IsValid_False_WhenManyDuplicateRelationshipName()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            DMRelationship dmRelationship = dmClass.OwnerRelationships.CreateBusinessObject();
            DMRelationship dmRelationship2 = dmClass.OwnerRelationships.CreateBusinessObject();
            DMRelationship dmRelationship3 = dmClass.OwnerRelationships.CreateBusinessObject();
            string relationshipName = TestUtilsShared.GetRandomString();
            dmRelationship.RelationshipName = relationshipName;
            dmRelationship2.RelationshipName = TestUtilsShared.GetRandomString();
            dmRelationship3.RelationshipName = relationshipName;
            IBusinessObjectRule rule = new DMClassDuplicateRelationshipNameRule();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmClass);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            StringAssert.Contains("The Entity/Class '", rule.Message);
            StringAssert.Contains("'" + dmClass.ToString() + "'", rule.Message);
            StringAssert.Contains("' cannot have more than one relationship with the same name('", rule.Message);
        }
        #endregion
    }
}